package cn.loioi.activiti;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;

import cn.virens.config.SpringActivitiConfig;
import cn.virens.config.SpringDataSourceConfig;
import cn.virens.web.components.activiti.ActivitiService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringDataSourceConfig.class, SpringActivitiConfig.class })
public class UnitActivitiServiceTest3 {
	private final Logger logger = LoggerFactory.getLogger(UnitActivitiServiceTest3.class);
	private @Resource ActivitiService activitiService;

	@Test
	public void deploymentProcess() throws IOException {
		activitiService.deploymentClasspathResource("activiti.bpmn");
	}

	/**
	 * 启动一个流程
	 * @throws IOException
	 */
	@Test
	public void processStart() throws IOException {
		Map<String, Object> variables = new HashMap<>();
		variables.put("applyUser", "user1");
		variables.put("applyUserName", "用户1");

		variables.put("applyTime", "2017-03-20 12:12:12");

		variables.put("candidateUser", "user2");
		variables.put("candidateDepartment", "用户2");

		variables.put("status", 0);

		activitiService.startProcessInstanceByKey("testProcess", null, "user1", variables);
	}

	/**
	 * 查询自己发起的流程
	 * @throws IOException
	 */
	@Test
	public void startedBy() throws IOException {
		HistoricProcessInstanceQuery query = activitiService.getHistoryService().createHistoricProcessInstanceQuery();
		query.startedBy("user1");
		query.includeProcessVariables();
		query.orderByProcessInstanceStartTime().desc();

		List<HistoricProcessInstance> list = query.list();

		for (HistoricProcessInstance process : list) {
			logger.debug("流程ID->" + process.getId());
			logger.debug("流程信息->" + JSON.toJSONString(process));
			logger.debug("流程变量->" + JSON.toJSONString(process.getProcessVariables()));

			List<Map<String, Object>> tasks = activitiService.getProcessTasks(process.getId());

			for (Map<String, Object> task : tasks) {
				logger.debug("任务信息->" + JSON.toJSONString(task));
			}

			logger.debug("------------------------------------------------------------\n");
		}
	}

	/**
	 * 查询我参与的流程
	 * @throws IOException
	 */
	@Test
	public void involvedUser() throws IOException {
		HistoricProcessInstanceQuery query = activitiService.getHistoryService().createHistoricProcessInstanceQuery();
		query.involvedUser("user2");
		query.includeProcessVariables();
		query.orderByProcessInstanceStartTime().desc();

		List<HistoricProcessInstance> list = query.list();

		for (HistoricProcessInstance process : list) {
			logger.debug("流程ID->" + process.getId());
			logger.debug("流程信息->" + JSON.toJSONString(process));
			logger.debug("流程变量->" + JSON.toJSONString(process.getProcessVariables()));

			List<Map<String, Object>> tasks = activitiService.getProcessTasks(process.getId());

			for (Map<String, Object> task : tasks) {
				logger.debug("任务信息->" + JSON.toJSONString(task));
			}

			logger.debug("------------------------------------------------------------\n");
		}
	}

	@Test
	public void processClaim() throws IOException {
		Task task = activitiService.getTask("5001", "user2", "");
		logger.debug("任务ID->" + task.getId());

		activitiService.claimTask(task.getId(), "user2");
		activitiService.setTaskVariable(task.getId(), "handleTime", "2017-03-22 12:12:12");
		activitiService.setTaskVariable(task.getId(), "handleUser", "user2");
		activitiService.setTaskVariable(task.getId(), "handleUserName", "用户2");
		activitiService.setTaskVariable(task.getId(), "handleDepartment", "department2");
		activitiService.setTaskVariable(task.getId(), "handleDepartmentName", "部门2");
		activitiService.setTaskVariable(task.getId(), "auditRemarks", "auditRemarks");

		Map<String, Object> variables = new HashMap<>();

		variables.put("status", 3);

		activitiService.completeTask(task.getId(), variables);
	}
}
